home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Educational Software Cooperative 4
/
Educational Software Cooperative 4.iso
/
trac
/
lesson4
< prev
next >
Wrap
Text File
|
1994-06-27
|
18KB
|
361 lines
ELEMENTARY COMPUTER PROGRAMMING
Lesson 4
Copyright 1990
Castle Oaks Computer Services
Post Office Box 36082
Indianapolis, IN 46236-0082
In the previous lessons, the necessary information was given for
writing a TRAC program, executing a TRAC program, and writing a TRAP
program. In this lesson, the information to assemble a TRAP program
will be given.
First, you must create the TRAP program according to the rules given
in Lesson 3. Then you will be ready to assemble the program. The
general command line format for using the assembler is:
TRAP [source file] [object file]
The parameters in brackets are optional. If either one is omitted,
the assembler will request the needed file descriptor. A file
descriptor may include a drive letter and subcatalogs (if required.)
If the object file descriptor is the same as that of an already
existing file, the new object file being created will replace the
existing one. There are no limitations on the file extensions for
either the source file or object file; however, it is recommended
that you use a file extension of 'TRA' for source files and 'OBJ'
for object files. As you accumulate files, these extensions will
help you to identify the types of the files.
The assembler is a two pass assembler. On the first pass, all
locations are assigned a numeric memory address and those with
mnemonic names will be entered into a symbol table for later use.
The symbol table can hold only 200 different symbols. Since this is
a large number no provision has been included to detect symbol table
overflow. Should you have too many symbols, you will probably "lock
up" the computer. After the copyright notice appears, the next
thing that displays is an "End of Pass 1" message.
On the second pass, mnemonic operation codes are assigned numeric
values; and mnemonic or relative addresses are assigned their numer-
ical equivalents. Each line of assembled code is displayed along
with the source code that generated it. During this pass, if any
syntactical errors are discovered, an appropriate error message will
be displayed, some action will be taken, and the assembly will
continue. An error message will appear before the line of code that
contains the error. At the end of pass 2, if any errors were de-
tected, the number of errors will be displayed. Also during pass 2,
the object file will be written. An object file will be created
even if the program contains errors. Therefore, if you try to
execute a program with syntactical errors the program may abort or
something more serious may happen (like "crashing" the computer.)
IV-1
When an assembly has been completed successfully, the object program
may be executed with TRAC using the methods outlined in Lesson 2.
(Note: Even if a program does not have any syntactical errors, it
may have logical errors. There is no way that an assembler can
detect your logical errors. It is strongly recommended that you
manually perform one or more test cases for any programs you
create.)
In Lesson 3, an example program was shown in its source form (Figure
III-8) and the resulting object code (Figure III-8a). Note that the
source code is carried over into the object file and when the pro-
gram is executed, everything beyond the first 15 columns is treated
as a comment.
In lesson 1, a flow chart (Figure I-4) and TRAC code (Figure I-5)
were given of a method for converting an octal number to decimal.
The same program is given here in Figure IV-1 and is included in the
software package as FIGIV-1.TRA (see next page). Note that the code
generated by the assembler is identical to that that was hand coded
for Figure I-5. This resulted because care was taken in re-origin-
ing various parts of the program to force this to happen. There was
no necessity for doing so except to illustrate that we could force
it to happen. You are encouraged to take this source code and make
other memory assignments, even rearrange the code to see how the
memory assignments change or not. Although you have considerable
latitude in making assignments there are some limitations. Note
that in the program, where relative addressing was used, the actual
number, 2, had to be used whereas elsewhere the index register was
referred to by the mnemonic name, I. Therefore, it was necessary
that I be made equivalent to 2. Also the definition for the read
area was placed last. This was done so that we didn't have to
explicitly reserve locations 1002, 1003, and 1004. The re-origining
at 0999 could be omitted and the program would function as before
but the read area would start at location 1506 instead of 1000. Try
this, execute the resulting program and note that it will function
the same. Another possible modification would be to move the two
lines,
0001 0
I 0
so they are the first two lines of the code and then you could omit
the re-origin lines 0009, 1499, and 0999. This would cause the
object program to use consecutive locations. STRT would now be
location 0003, the actual code would occupy through location 0041,
the constants and variables would use 0042 through 47, and the read
area would start at location 0048.
IV-2
-----------------------------------------------------------------
0009 0 Origin
STRT RD X Read X (Also sets next 4 locations to zero)
LD X Load accumulator with X
BZSTOP If X is zero jump to halt instruction
BN*+4 If X is negative, skip next 3 instructions
LD ONE Load +1
ST S Store at S (sign)
BU*+6 Branch unconditionally around next 5
LDNEG1 Load -1
ST S Store at S
LDZERO Load zero
SU X Subtract X (This makes a negative X positive)
ST X Store back at X
LD ONE Load 1
ST I I=1 using index register 2
LD X Load X
SR0008 Shift it right 8
ST Y Store at Y
LOOP LD Y Load Y (Note this is the start of a loop)
MU ATE Multiply by 8
ST Y Store at Y
LD X Load X
2SL0000 Shift left by I (Contents of register 2)
SR0008 Shift right 8 (This isolates an octal digit)
AD Y Add previous Y
ST Y Store back at Y
LD I Load I
AD ONE Add 1
ST I Store at I
SUNINE Subtract 9 to see if done
BNLOOP If negative repeat loop
LD X Load X
MU S Multiply by sign
ST X Store at X
LD Y Load Y
MU S Multiply by sign
ST Y Store at Y
PC X Print X, Y
BUSTRT Branch back to start
STOP HT0000 Halt
0001 0 Re-origin to define I
I 0
1499 0 Re-origin for constants
ZERO 0 Zero
ONE 1 One
S 0
NEG1 -1 Negative one
ATE 8 Eight
NINE 9 Nine
0999 0 Re-origin for read area
X 0
Y 0
ENSTRT End of code and starting location
Figure IV-1
-----------------------------------------------------------------
IV-3
The following flow chart is a "bubble" sort. You enter 10 values
(using two reads) then the program makes multiple passes through the
data exchanging values where needed until a pass is completed with
no exchanges. The sorted values are then printed.
|
v
--------------------------------
/ Read A(0),A(1),A(2),A(3),A(4) /
--------------------------------
|
v
--------------------------------
/ Read A(5),A(6),A(7),A(8),A(9) /
--------------------------------
|
v
+-----+
| I=0 |<---------------+
| F=0 | |
| J=1 | |
+-----+ |
| |
v |
/ \ |
/ \ |
/ \ <= |
+---><A(I): >-----+ |
| \ A(J)/ | |
| \ / | |
| \ / | |
| | > | |
| v | |
| +-------------+ | |
| | T=A(I) | | | +-------+
| | A(I)=A(J) | | | | STOP |
| | A(J)=T | | | +-------+
| +-------------+ | | ^
| | | | |
| v | | -------------------------
| +-----+ | | / Print /
| | F=1 | | | /A(5),A(6),A(7),A(8),A(9)/
| +-----+ | | -------------------------
| | | | ^
| v | | |
| +-------+ | | |
| | J=J+1 |<----+ | -------------------------
| | I=I+1 | | / Print /
| +-------+ | /A(0),A(1),A(2),A(3),A(4)/
| | | -------------------------
| v not = | ^
| < / \ >= / \ = |
+------<I:9>--------------><F:0>-----------+
\./ \./
Exercise IV-1
IV-4
Experiment with exercise IV-1. For example, you do not really need
to have an explicit J. It is always equal to I+1. You can elimi-
nate it in the following manner. When you reserve space for A, you
can give the location following A(0) a different name, say A1. Then
everyplace that calls for A(J), you can use A1(I). This technique
can be used to facilitate the second read and the second print.
That is, give the memory location that corresponds to A(5) a name
such as A5. Then you can say RD A5 and PC A5 in order to read and
print the second set of A's.
|
v
+---------+
| T(0)=3 |
| X=5 |
| J=1 |
+---------+
|
+-------+ v
| STOP | ----------
+-------+ / Read XF /
^ ----------
| > |
/ \ v
/ \ <= +-----+
<X:XF >----------------->| I=0 |<---------+
\ / +-----+ |
\./ | |
^ v |
| +------------+ |
+-------+ | Q=X/T(I) | |
| X=X+2 |<------+ | R=X-Q*T(I) | |
+-------+ | +------------+ |
^ | | |
| | v |
---------- | = / \ |
/ Print X / +----------<R:0> |
---------- \./ |
^ | Not = |
| v |
| --------- |
+-------+ +--------+ < / \ >= +-------+
| J=J+1 |<-| T(J)=X |<-<X:T(I)*T(I)>---| I=I+1 |
+-------+ +--------+ \ / +-------+
---------
Exercise IV-2
In exercise IV-2 you will build and display a table of prime numbers
(i.e. numbers which are divisible only by themselves and one.) In
the first box of the flow chart, certain quantities are initialized.
You may either do this within the executable program or, you may
initialize them by entering their initial values when you reserve
memory space for them.
IV-5
The program requests a final value. This provides an upper limit
for the execution. Should you wish to terminate earlier, you may
abort the program by using ^C (control-C).
The program starts with only the value, 3, in the table (even though
2 is also a prime, it is the only even prime and therefore we will
only test the odd numbers.) Starting with a value of 5, each odd
number is divided by each value in the table whose square does not
exceed the number being tested to see if there is a remainder. If
there is no remainder, the number is not a prime and the program
advances to the next number to be tested. If there is a remainder
for all tests made, the number is prime and the value is added to
the table for possible later use and the next odd number is tested,
etc.
Code this exercise in TRAP, assemble it, and run it. Note that you
do not have to actually store the values, Q and R. Try this varia-
tion and other variations you might think of to gain a better under-
standing of the use of an assembler and machine code.
As further exercises, re-code Figures II-5 and II-8 in TRAP. Assem-
ble and execute them.
We hope you enjoyed learning TRAC and TRAP.
IV-6